package com.mrx.xfriendship.converter;

import com.mrx.xfriendship.model.dto.PhotoWallDTO;
import com.mrx.xfriendship.model.dto.UserDetailDTO;
import com.mrx.xfriendship.model.dto.UserInfoDTO;
import com.mrx.xfriendship.model.po.UserDetailPO;
import com.mrx.xfriendship.model.vo.UpdateUserVO;
import com.mrx.xfriendship.model.vo.UserInfoVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mr.X
 * @since 2023-01-15 14:43
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING,
        uses = OtherConverter.class,
        imports = LocalDateTime.class
)
public abstract class UserDetailConverter implements IConverter<UserDetailPO, UserDetailDTO> {

    @Resource
    private OtherConverter otherConverter;

    @Override
    @Mapping(target = "contactWay", expression = "java( po.getContact() )")
    @Mapping(target = "ipLocation", expression = "java( po.getIpLocation() )")
    public abstract UserDetailDTO toDTO(UserDetailPO po);

    public List<PhotoWallDTO> toPreviewPhotos(List<PhotoWallDTO> list) {
        return list.stream()
                .peek(it -> it.setUrl(otherConverter.toPreviewAvatar(it.getUrl())))
                .collect(Collectors.toList());
    }

    public abstract UserInfoDTO toUserInfo(UserDetailPO po);

    @Mapping(target = "updateTime", expression = "java( LocalDateTime.now() )")
    public abstract UserDetailPO toUserDetail(Integer id, UserInfoVO info);

    @Mapping(target = "role", ignore = true)
    @Mapping(target = "contact", expression = "java( vo.getContact() )")
    public abstract UserDetailPO toPOForUpdate(UpdateUserVO vo);

}
